﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FFS.BLL;
using FFS.Model;

namespace FFS
{
    public partial class frmFamilyMember : Form
    {
        public frmFamilyMember()
        {
            InitializeComponent();
        }

        FamilyMemberInfo fm = new FamilyMemberInfo();
        List<FamilyMemberInfo> fm_list = new List<FamilyMemberInfo> ();
        FamilyMemberService fmService = new FamilyMemberService();

        private string actionType = "Add";

        private void BindListView(List<FamilyMemberInfo> list_familyMember)
        {
            string[] familyMemberArray = new string[5];

            this.listView_FamilyMember.Items.Clear();

            foreach (FamilyMemberInfo familymember in list_familyMember)
            {
                if (familymember != null)
                {
                    familyMemberArray[0] = familymember.FamilyMemberID.ToString().Trim();
                    familyMemberArray[1] = familymember.FamilyMemberName;
                    familyMemberArray[2] = familymember.FamilyMemberTitle;
                    familyMemberArray[3] = familymember.Input_User.ToString().Trim();
                    familyMemberArray[4] = familymember.Input_Date.ToString().Trim();
                    ListViewItem item = new ListViewItem(familyMemberArray);
                    listView_FamilyMember.Items.Add(item);
                }
            }
        }

        private void frmFamilyMember_Load(object sender, EventArgs e)
        {
            this.cmb_searchItem.SelectedIndex = 0;
            this.cmb_search_compare.SelectedIndex = 0;
            BindListView(fmService.ManageFamilyMember().GetAllFamilyMember());
        }

        private void tsb_add_Click(object sender, EventArgs e)
        {
            this.actionType = "Add";
            this.txt_FamilyMemberName.Text = "";
            this.txt_FamilyMemberTitle.Text = "";
            panel_editFamilyMember.Visible = true;
            this.tsmi_edit.Enabled = false;
            this.tsmi_delete.Enabled = false;
        }

        private void tsb_edit_Click(object sender, EventArgs e)
        {
            if (listView_FamilyMember.SelectedItems.Count == 1)
            {
                this.actionType = "Edit";
                panel_editFamilyMember.Visible = true;
                this.tsmi_add.Enabled = false;
                this.tsmi_delete.Enabled = false;
            }
            else
            {
                MessageBox.Show("请选择要修改的记录","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

        private void listView_FamilyMember_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.actionType = "Edit";
                if (actionType == "Edit")
                {
                    this.txt_FamilyMemberName.Text = listView_FamilyMember.SelectedItems[0].SubItems[1].Text;
                    this.txt_FamilyMemberTitle.Text = listView_FamilyMember.SelectedItems[0].SubItems[2].Text;
                }
            }
            catch (Exception)
            {

            }
        }

        private void tsb_delete_Click(object sender, EventArgs e)
        {
            if (listView_FamilyMember.SelectedItems.Count == 1)
            {
                fmService.ManageFamilyMember().DeleteFamilyMember(int.Parse(listView_FamilyMember.SelectedItems[0].SubItems[0].Text));
                MessageBox.Show("删除成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                btn_Search_Click(sender, e);
            }
            else
            {
                MessageBox.Show("请选择要删除的记录","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

        private void tsb_search_Click(object sender, EventArgs e)
        {
            panel_search.Visible = !panel_search.Visible;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            panel_editFamilyMember.Visible = false;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (this.txt_FamilyMemberName.Text.ToString().Trim() == "")
            {
                MessageBox.Show("家庭成员姓名不能为空！","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.txt_FamilyMemberName.Focus();
            }
            else if (this.txt_FamilyMemberTitle.Text.ToString().Trim() == "")
            {
                MessageBox.Show("家庭成员称谓不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txt_FamilyMemberTitle.Focus();
            }
            else
            {
                fm.FamilyMemberName = this.txt_FamilyMemberName.Text.ToString().Trim();
                fm.FamilyMemberTitle = this.txt_FamilyMemberTitle.Text.ToString().Trim();
                fm.Input_User = "0000";


                if (this.actionType == "Add")
                {
                    if (fmService.ManageFamilyMember().CreateFamilyMember(fm) == 1)
                    {
                        MessageBox.Show("添加家庭成员 [" + this.txt_FamilyMemberName.Text.ToString().Trim() + "] 成功","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.txt_FamilyMemberName.Text = "";
                        this.txt_FamilyMemberTitle.Text = "";
                        btn_Search_Click(sender,e);
                    }
                    else 
                    {
                        MessageBox.Show("添加家庭成员 [" + this.txt_FamilyMemberName.Text.ToString().Trim() + "] 失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (this.actionType == "Edit")
                {
                    fm.FamilyMemberID = int.Parse(this.listView_FamilyMember.SelectedItems[0].SubItems[0].Text.ToString().Trim());
                    if (fmService.ManageFamilyMember().ModifyFamilyMember(fm) == 1)
                    {
                        MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btn_Search_Click(sender, e);
                        this.actionType = "";
                        this.panel_editFamilyMember.Visible = false;
                        this.btn_Search_Click(sender,e);
                    }
                    else
                    {
                        MessageBox.Show("修改失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            if (this.txt_searchContent.Text.ToString().Trim() != "")
            {
                string string_searchCondition = " and 1=1 ";
                if (this.cmb_searchItem.SelectedIndex == 0)
                {
                    if (this.cmb_search_compare.SelectedIndex == 0)
                    {
                        string_searchCondition = " FamilyMemberName like N'%" + this.txt_searchContent.Text.ToString().Trim() + "%' "; ;
                    }
                    else
                    {
                        string_searchCondition = " FamilyMemberName = N'" + this.txt_searchContent.Text.ToString().Trim() + "' ";
                    }
                }
                else
                {
                    if (this.cmb_searchItem.SelectedIndex == 0)
                    {
                        if (this.cmb_search_compare.SelectedIndex == 0)
                        {
                            string_searchCondition = " FamilyMemberTitle like N'%" + this.txt_searchContent.Text.ToString().Trim() + "%' "; ;
                        }
                        else
                        {
                            string_searchCondition = " FamilyMemberTitle = N'" + this.txt_searchContent.Text.ToString().Trim() + "' ";
                        }
                    }
                }

                BindListView(fmService.ManageFamilyMember().GetFamilyMemberBySearchCondition(string_searchCondition));
            }
            else
            {
                BindListView(fmService.ManageFamilyMember().GetAllFamilyMember());
            }
        }

        private void tsmi_add_Click(object sender, EventArgs e)
        {
            tsb_add_Click(sender,e);
        }

        private void tsmi_edit_Click(object sender, EventArgs e)
        {
            tsb_edit_Click(sender,e);
        }

        private void tsmi_delete_Click(object sender, EventArgs e)
        {
            tsb_delete_Click(sender,e);
        }

        private void tsmi_search_Click(object sender, EventArgs e)
        {
            tsb_search_Click(sender,e);
        }

        private void txt_searchContent_KeyUp(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btn_Search;
        }

        private void txt_FamilyMemberName_KeyUp(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btn_Save;
        }

        private void txt_FamilyMemberTitle_KeyUp(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btn_Save;
        }
    }
}
